package com.zhangxu.microservice.common.result;

public class CommonResult<T> implements java.io.Serializable {

    private static final long serialVersionUID = 344400440420827850L;
    private Integer code;
    private T data;
    private String message;

    public boolean checkSuccess() {
        return CommonResult.ResultCode.SUCCESS.code.equals(this.code);
    }

    public CommonResult() {
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ResultCode.SUCCESS.code, ResultCode.SUCCESS.message, null);
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.code, ResultCode.SUCCESS.message, data);
    }

    public static <T> CommonResult<T> error(String errorMsg) {
        return new CommonResult<T>(ResultCode.SYSTEM_ERROR.code, errorMsg, null);
    }

    public static <T> CommonResult<T> error(Integer resultCode, String errorMsg) {
        return new CommonResult<T>(resultCode, errorMsg, null);
    }

    public Integer getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public String getErrorMsg() {
        return this.message;
    }

    public CommonResult<T> setResultCode(Integer resultCode) {
        this.code = resultCode;
        return this;
    }

    public CommonResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public CommonResult<T> setErrorMsg(String errorMsg) {
        this.message = errorMsg;
        return this;
    }

    public enum ResultCode {
        SUCCESS(200, "调用成功"),
        SYSTEM_ERROR(500, "系统错误");

        private final Integer code;
        private final String message;

        ResultCode(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        public Integer getCode() {
            return this.code;
        }

        public String getMessage() {
            return this.message;
        }
    }
}
